package cn.Y_Qiu.org.service.impl;

import cn.Y_Qiu.basic.exception.BusinessException;
import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.org.domain.Employee;
import cn.Y_Qiu.org.domain.Shop;
import cn.Y_Qiu.org.mapper.EmployeeMapper;
import cn.Y_Qiu.org.mapper.ShopMapper;
import cn.Y_Qiu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl  extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void regist(Shop shop) {
        // 从前端传来的shop中获取要上传的管理员employee信息
        Employee admin = shop.getAdmin();
        // 判断传入的两个密码是否一致(前端已经判断，此处判断防止不是从前端传来的请求出现密码和重复密码不一致)
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        //查询数据库中的用户名是否重复
        Employee employee = employeeMapper.queryByUsername(admin.getUsername());
        //
        if (employeeMapper.queryByUsername(admin.getUsername()) != null){
            throw new BusinessException("账号已存在");
        }
        // 回传了id存入Employess admin
        employeeMapper.add(admin);
        // 设置shop的Admin_id
        shop.setAdmin_id(admin.getId());
        // 重置shop内的admin对象
        shop.setAdmin(admin);
        // 添加shop到数据库,并获取回传的id
        shopMapper.add(shop);
        // 修改admin里的shopid
        admin.setShop_id(shop.getId());
        // 重置数据库内admin的shopid
        employeeMapper.update(admin);
    }

    @Override
    public void shopsettlement(Shop shop) {
        shopMapper.shopsettlement(shop);
    }
}
